#include "solve.h"
#include "matrix.h"

void power(Matrix a)        //幂法求矩阵最大的特征值和对应的特征向量
{
    Matrix u(a.row, 1, 1);
    double m = -1;
    double eps = 1e-7;
    Matrix y(a.row, 1, 1);
    while (vnorm(a*u-u*m)>eps)
    {
        y = a * u;
        m = y.norm0();
        u = y * (1 / m);
    }
    std::cout << m << std::endl;
    u.show();
}

void arcpower(Matrix a)     //反幂法求矩阵最小的特征值和对应的特征向量
{
    Matrix u(a.row, 1, 1);
    double m = -1;
    double eps = 1e-7;
    Matrix y(a.row, 1, 1);
    Matrix b = a.arc();
    while (vnorm(b*u-u*m)>eps)
    {
        y = b * u;
        m = y.norm0();
        u = y * (1 / m);
    }
    std::cout << 1/m << std::endl;
    u.show();
}

/*int main()
{
    double p[9] = {4, 1, 0, 1, 4, 1, 0, 1, 4};
    Matrix a(3, 3, p);
    power(a);
    arcpower(a);
}*/